﻿//=====================================================
// Copyright © 2017-2018 xuxuzhaozhao
// All rights reserved
// GUID: 112282c7-3ee8-47cd-867b-6506a5ef6b43
// CLR版本: 4.0.30319.42000
// 命名空间名称: xuxuzhaozhao.Blog.Application.XuAritcleService.Dto.ArticleType
// 文件名: PageInput
// 当前系统时间: 2017/11/8 13:45:36
// 当前登录用户名: xuxuzhaozhao
//======================================================

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json;
using xuxuzhaozhao.Blog.Domain;
using xuxuzhaozhao.Blog.Infrastructure.XuTools;
using System.Linq;
using System.Reflection;

namespace xuxuzhaozhao.Blog.Application.XuAritcleService.Dto.Article
{
    public class PageInput
    {
        [JsonProperty("index")]
        public int Index { get; set; } = 0;

        [JsonProperty("size")]
        public int Size { get; set; } = 15;

        [JsonProperty("conditions")]
        public Conditions Conditions { get; set; }

        [JsonProperty("sort")]
        public List<Sort> Sort { get; set; }
        
        /// <summary>
        /// 组装条件语句
        /// </summary>
        [JsonIgnore]
        public Expression<Func<XuArticle, bool>> WhereLambda
        {
            get
            {
                Expression<Func<XuArticle, bool>> x = (y => true);
                if (Conditions.ArticleType > 0)
                {
                    x.AndAlso(t => t.ArticleType == Conditions.ArticleType);
                }
                if (!string.IsNullOrEmpty(Conditions.Title))
                {
                    x.AndAlso(t => t.Title.Contains(Conditions.Title));
                }
                if (!string.IsNullOrEmpty(Conditions.Content))
                {
                    x.AndAlso(t => t.Content.Contains(Conditions.Content));
                }
                if (!string.IsNullOrEmpty(Conditions.Author))
                {
                    x.AndAlso(t => t.Creator.Equals(Conditions.Author));
                }
                return x;
            }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        [JsonIgnore]
        public string SortLambda
        {
            get
            {
                var ascStr = "";
                var descStr = "";
                foreach (var sort in Sort)
                {
                    if (sort.Type.ToUpper().Equals("ASC"))
                    {
                        ascStr += $"{sort.Name.StripSql()},";
                    }
                    else
                    {
                        descStr += $"{sort.Name.StripSql()},";
                    }
                }
                if (string.IsNullOrEmpty(ascStr) && string.IsNullOrEmpty(descStr)) return "ID ASC";

                if (!string.IsNullOrEmpty(ascStr) && string.IsNullOrEmpty(descStr)) return $"{ascStr.TrimEnd(',')} ASC";

                if (string.IsNullOrEmpty(ascStr) && !string.IsNullOrEmpty(descStr)) return $"{descStr.TrimEnd(',')} DESC";

                return $"{ascStr.TrimEnd(',')} ASC,{descStr.TrimEnd(',')} DESC";
            }
        }
    }
    public class Sort
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }
    }
    public class Conditions
    {
        public int ArticleType { get; set; } = 0;
        public string Title { get; set; }
        public string Content { get; set; }
        public string Author { get; set; }
    }
}
